import {
    sandBox,
    config,
    is
} from '../../../lib/myapp.js'
Page({
    data:{
        memberList:[],
        select_member:{},
        canChoose:false,
        url:'',
        description:''
    },
    onLoad(e){
        wx.showLoading()



        this.setData({
            canChoose:!!e.url ? true : false,
            url:!!e.url ? e.url : '',
            tag:!!e.tag ? e.tag : ''
        })
   

        this.getMembers();

    },
    searchMember(e){
        var username = e.detail.value
        var oauth = wx.getStorageSync('user_token');
        if (!oauth) {
            wx.showModal({
                content:'请先登录',
                success:(res)=>{
                    if (res.confirm) {
                        var route = getCurrentPages()[getCurrentPages().length - 1].route
                        wx.navigateTo({
                            url:`/pages/user/login/login?url=${route}`
                        })
                    }
                }
            })
        }
        sandBox.post({
            api:`api/O2O/user/search`,
            data:{
                username:username
            },
            header: {Authorization: oauth},
        }).then((res)=>{
            var data = res.data
            var result = []
            if (data.status && data.data) {
                result.push(data.data)
                this.setData({
                    memberList:result
                })
            } else {

                this.setData({
                    memberList:[],
                    description:'未找到该会员'
                })

            }
        }).catch(()=>{
            wx.showModal({
                content:'数据获取失败'
            })
        })
    },
    sure(){
        if (!is.isEmptyObject(this.data.select_member)) wx.setStorageSync('select_member',this.data.select_member)
        if (!!this.data.tag) {
            wx.navigateTo({
                url:`/pages/user/changeScore/changeScore?type=${this.data.tag}`
            })
        } else {
            wx.navigateBack()
        }


    },
    cancel(){
        wx.navigateBack();
    },
    selectMember(e){
        var index = e.detail.value
        var list = this.data.memberList

        list[0][index].checked = true
        this.setData({
            select_member:list[0][index]
        })

    },
    getMembers(){
        var oauth = wx.getStorageSync('user_token');
        var clerk = wx.getStorageSync('clerk')

        if (!oauth) {
            wx.showModal({
                content:'请先登录',
                success:(res)=>{
                    if (res.confirm) {
                        wx.navigateTo({
                            url:`/pages/user/login/login`
                        })
                    }
                }
            })
        }

        var shop_id = clerk.shop_id;



        sandBox.get({
            api:`api/O2O/user/list?shop_id=${shop_id}`,
            header: {Authorization: oauth},
        }).then((res)=>{
           var data = res.data;
            if (data.status && data.data) {

                var list = data.data.map((item)=>{
                    return Object.assign(item,{checked:false})
                })
                this.setData({
                    memberList:[list] || []
                })
                wx.hideLoading()
            } else {
                wx.hideLoading()
                wx.showModal({
                    content:data.message || '暂无会员',
                    success:(confirm)=>{
                        if (confirm.confirm) {
                            wx.navigateBack()
                        }
                    }
                })
            }
        })
    },
    inputSearch(e){
        var value = e.detail.value

        if (!value && value === '') {
            wx.showLoading();
            this.getMembers()
        }
    },
    toMemberDetail(e){
        console.log(e)
        var userId = e.currentTarget.dataset.userid
        console.log(userId)
        wx.navigateTo({
            url:`/pages/member/index/index?userId=${userId}`
        })

    }

})
